package com.ap.core.email.message.processor.impl;

import com.ap.core.cxf.email.jaxb.common.MessageStatusType;
import com.ap.core.cxf.email.jaxb.managemessage.SendEmail1Request;
import com.ap.core.cxf.email.jaxb.managemessage.SendEmail1Response;
import com.ap.core.email.message.translator.api.InfoTranslator;
import com.ap.core.email.api.EmailPayloadInfo;
import com.ap.core.email.api.EmailService;
import com.ap.core.exception.BusinessFailureException;
import com.ap.core.message.service.CoreResponseInfo;
import com.ap.core.message.service.MessageProcessor;
import org.joda.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * User: Ranjit.Singh
 * Implementation class for {@link MessageProcessor}
 */
public class EmailMessageProcessor<AI> implements MessageProcessor<SendEmail1Request, SendEmail1Response, AI> {

    private final static Logger LOG = LoggerFactory.getLogger(EmailMessageProcessor.class);

    private EmailService emailService;
    private InfoTranslator<SendEmail1Request,EmailPayloadInfo> infoTranslator;


    /**
     * {@inheritDoc}
     * This method is the entry point to start the data base transaction.
     * @param request  - request object to be processed
     * @param response - response object returned
     * @return  - response message returned.
     */
    @Override
    public CoreResponseInfo<SendEmail1Response, AI> process(final SendEmail1Request request, final SendEmail1Response response) throws BusinessFailureException {

       //Validate template name and participant elements existence in request
       validate(request);

       final EmailPayloadInfo emailPayloadInfo = infoTranslator.translate(request);

       emailService.send(emailPayloadInfo);

       //Send Acknowledgement as response
       final SendEmail1Response sendEmail1Response = new SendEmail1Response();
       sendEmail1Response.setMessageStatus(MessageStatusType.RECD);
       sendEmail1Response.setMsgTimestamp(new LocalDateTime());
       return new CoreResponseInfo<SendEmail1Response, AI>() {
           @Override
           public SendEmail1Response getResponse() {
               return sendEmail1Response;
           }

           @Override
           public AI getAdditionalInfo() {
               return null;
           }
       };

    }

    public void setEmailService(EmailService emailService) {
        this.emailService = emailService;
    }

    public void setInfoTranslator(InfoTranslator<SendEmail1Request, EmailPayloadInfo> infoTranslator) {
        this.infoTranslator = infoTranslator;
    }

    private void validate(final SendEmail1Request request) throws BusinessFailureException{
         if(null == request.getTemplateName() && (null == request.getParticipantId() || null == request.getParticipantType())){

             throw new BusinessFailureException("If template name is not provided in email request then participant id and type must be provided");

         }
    }
}